# -*- coding: utf-8 -*-
__author__ = 'lychiyu'
__date__ = '17-9-8 下午3:41'

import re

from django import forms
from operation.models import UserAsk


class UserAskForm(forms.ModelForm):
    class Meta:
        model = UserAsk
        fields = ['name', 'mobile', 'course_name']

    # 自定义mobile的验证规则,注意方法的命名规则
    def clean_mobile(self):
        # 取得表单内的mobile值
        mobile = self.cleaned_data['mobile']
        # 手机号验证规则
        REFEX_MOBILE = "^1[358]\d{9}$|^147\d{8}$|^176\d{8}$"
        p = re.compile(REFEX_MOBILE)
        if p.match(mobile):
            return mobile
        else:
            raise forms.ValidationError("手机号码非法", code="mobile_invalid")
